package com.example.zk.client;

import org.apache.zookeeper.*;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;

/**
 * Created by SOAL on 2017/7/9 0009.
 */
public class DisSerer {
    public static final String connectString = "192.168.1.142:2181,192.168.1.140:2181,192.168.1.143:2181";
    public static final int sessionTimeout = 50000;
    public static final String parentNode = "/servers";




    ZooKeeper zkClent = null;


    public void getConnect() throws Exception {
        zkClent = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                //收到事件通知后回调函数（自己事件处理逻辑）
                System.out.println(watchedEvent.getType() + "----" + watchedEvent.getPath());
//                try {
//                    zkClent.getChildren("/",true);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            }
        });
    }

    public  void mybuss(String hostname) throws InterruptedException {
        System.out.println(hostname+" -- is running");
        Thread.sleep(   Long.MAX_VALUE);

    }

    public  void regsterServer(String hostname) throws Exception {
        String sss=zkClent.create(parentNode+"/server1",hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println( hostname+"  online "+sss);
    }

    public static void main(String[] args) throws Exception {
        DisSerer disSerer=new DisSerer();
        disSerer.getConnect();

        String ssd="ddddr";
        disSerer.regsterServer(ssd);
        disSerer.mybuss(ssd);


    }
}
